package jin.chen.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 解决跨越问题
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter(){
        //先设置configuration
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //设置允许访问的地址
        corsConfiguration.addAllowedOrigin("http://127.0.0.1:8020");
        corsConfiguration.addAllowedOrigin("http://localhost:8020");
        corsConfiguration.addAllowedOrigin("http://49.234.207.183");
        //是否允许发送cookie信息
        corsConfiguration.setAllowCredentials(true);
        //设置允许的方法
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedHeader("*");

        //映射的Url路径，运行访问的
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }
}
